package com.aifeng.hassan_food.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.LinearLayout
import com.aifeng.base.BaseActivity
import com.aifeng.base.addItems
import com.aifeng.common_ui.base.getColorEx
import com.aifeng.common_ui.base.gone
import com.aifeng.common_ui.base.visible
import com.aifeng.common_ui.helper.dp2px
import com.aifeng.common_ui.view.FullyLinearLayoutManager
import com.aifeng.hassan_food.R
import com.aifeng.model.ExpressData
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.getExtra
import com.aifeng.utils.subscribeByFinally
import com.aifeng.viewmodel.OrderViewModel
import kotlinx.android.synthetic.main.activity_order_express.*
import kotlinx.android.synthetic.main.layout_empty.*
import net.idik.lib.slimadapter.SlimAdapter
import org.koin.androidx.viewmodel.ext.android.getViewModel

class OrderExpressActivity : BaseActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order_express)
        initTitle(title = "物流信息", divider = false)

        getViewModel().expressList(intent.getExtra("num"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.clear()
                    mList.addItems(it)
                    mAdapter.updateData(mList)

                    if (mList.isNotEmpty()) {
                        val item = mList[0] as ExpressData
                        express_name.text = item.opeName
                        express_num.text = "运单号：${item.waybillCode}"
                    }
                },
                onFinally = {
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

    override fun getViewModel() = getViewModel<OrderViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        empty_img.apply {
            layoutParams = LinearLayout.LayoutParams(
                dp2px(80f),
                dp2px(80f)
            ).apply {
                setMargins(0, dp2px(180f), 0, 0)
            }
        }
        empty_hint.text = "暂无相关物流信息"

        express_list.apply {
            isNestedScrollingEnabled = false
            layoutManager = FullyLinearLayoutManager(baseContext)
            mAdapter = SlimAdapter.create()
                .register<ExpressData>(R.layout.item_express_list) { inner, jector ->
                    val index = mList.indexOf(inner)
                    jector.text(R.id.item_express_content, inner.opeRemark)
                        .text(R.id.item_express_time, inner.opeTime.replace("/", "-"))
                        .textColor(
                            R.id.item_express_content,
                            getColorEx(if (index == 0) R.color.blue else R.color.black)
                        )
                        .textColor(
                            R.id.item_express_time,
                            getColorEx(if (index == 0) R.color.blue else R.color.black)
                        )
                        .image(
                            R.id.item_express_img,
                            if (index == 0) R.mipmap.icon_express01 else R.mipmap.icon_express02
                        )
                }
                .attachTo(this)
        }
    }

}
